﻿using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using InsightAuto.Domain.Core;

namespace InsightAuto.Web.Controllers
{
    public class CarController : Controller
    {
        private CoreContainer db = new CoreContainer();

        public ActionResult Index(int? id)
        {
            return RedirectToAction("Manufacturer");
            //return View();
        }

        public ActionResult Manufacturer()
        {
            var groupByFirstPinyin = from m in db.Manufacturers.ToList()
                                     group m by Utility.FirstPinyinCharOf(m.Name);

            var dictionary = groupByFirstPinyin.ToDictionary(g => g.Key, g => g.ToList());

            return View(dictionary);
        }

        public ActionResult ManufacturerDetail(int id)
        {
            var manufacturer = db.Manufacturers.SingleOrDefault(m => m.ID == id);

            return View(manufacturer);
        }

        public ActionResult Brand(int id)
        {
            var brand = db.Brands
                .Include("Series.Models.Prices")
                .Include("Series.Models.Detail")
                .SingleOrDefault(o => o.ID == id);

            if (brand == null)
            {
                return HttpNotFound();
            }

            var seriesDictionary = new Dictionary<int, IEnumerable<IGrouping<int, Model>>>();
            foreach (var series in brand.Series)
            {
                var groupedModels = from m in series.Models
                                    orderby m.Detail.Displacement ascending
                                    group m by m.Year into g
                                    orderby g.Key descending
                                    select g;

                seriesDictionary.Add(series.ID, groupedModels);
            }

            ViewBag.SeriesDictionary = seriesDictionary;

            return View(brand);
        }

        public ActionResult Series(int id)
        {
            var series = db.Series
                .Include("Models.Prices")
                .Include("Models.Detail")
                .SingleOrDefault(o => o.ID == id);

            if (series == null)
            {
                return HttpNotFound();
            }

            return View(series);
        }

        public ActionResult Model(int id)
        {
            var model = db.Models
                .Include("Prices")
                .Include("Detail")
                .SingleOrDefault(o => o.ID == id);

            if (model == null)
            {
                return HttpNotFound();
            }

            return View(model);
        }

        public PartialViewResult Price()
        {
            return PartialView();
        }

        public ActionResult Class(int? id)
        {
            ViewBag.Id = id;
            if (id.HasValue)
            {
                return View();
            }
            else
            {
                return PartialView();
            }
        }

        public PartialViewResult SeriesDetail()
        {
            return PartialView();
        }

    }
}
